#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright @ 2014 OPS, Qunar Inc. (qunar.com)
#
# Author: tingfang.bao <tingfang.bao@qunar.com>
# DateTime: 15-3-10 下午5:50

# 需要安装pip install python-memcached

from werkzeug.contrib.cache import MemcachedCache
from flask import Flask

cache = MemcachedCache(['127.0.0.1:11211'])

app = Flask(__name__)


def calc():
    return '123'


@app.route("/")
def index():
    cache_key = 'calc'
    rv = cache.get(cache_key)
    if rv is None:
        print '从底层获取calc...'
        rv = calc()
        cache.set(cache_key, rv, timeout=5 * 60)
    return rv


if __name__ == '__main__':
    app.run(debug=True)